<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Agent;
use App\Models\Mem;
use App\Models\Order;

class Stat extends AdminController{
    
    public function index(Request $req){
        if ($req->method() == 'POST') {
            if ($_POST['op'] == 'rank') {
                $list = [];
                if ($_POST['type'] == 'money') {
                    $list = Order::from(Order::selectRaw("aid,sum(price) as num")->where('status','>',0)->groupBy('aid'))->select('aid','num')->orderByDesc('num')->get();
                    foreach ($list as &$v) {
                        $v->name = Agent::where('id',$v->aid)->value('mobile');
                    }
                }
                wxresp(1,['list'=>$list]);
            }
            
            /*
             总交易额，今日，本月
             总订单数，今日，本月
             总用户数，今日，本月
             总分销商数，今日，本月
             分销商总收益排行榜，总邀请下级排行榜，本月收益排行榜，本月邀请下级排行榜
             商城达人总数，达人收益总金额，达人收益排行榜
             */
            $lmon = date('Ym');
            if ($_POST['op'] == 'money') {
                $stat = array(
                        round(Order::where('status','>',0)->sum('price'),2),
                        round(Order::where('status','>',0)->whereRaw("to_days(from_unixtime(createtime))=to_days(now())")->sum('price'),2),
                        round(Order::where('status','>',0)->whereRaw("date_format(from_unixtime(createtime),'%Y%m')=".$lmon)->sum('price'),2),
                );
            }elseif ($_POST['op'] == 'profile') {
                $stat = array(
                        round(Order::where('status','>',0)->sum(\DB::raw('price-costprice-reback-preback')),2),
                        round(Order::where('status','>',0)->whereRaw("to_days(from_unixtime(createtime))=to_days(now())")->sum(\DB::raw('price-costprice-reback-preback')),2),
                        round(Order::where('status','>',0)->whereRaw("date_format(from_unixtime(createtime),'%Y%m')=".$lmon)->sum(\DB::raw('price-costprice-reback-preback')),2),
                );
            }elseif ($_POST['op'] == 'order') {
                $stat = array(
                        Order::where('status','>',0)->count(),
                        Order::where('status','>',0)->whereRaw("to_days(from_unixtime(createtime))=to_days(now())")->count(),
                        Order::where('status','>',0)->whereRaw("date_format(from_unixtime(createtime),'%Y%m')=".$lmon)->count(),
                );
            }elseif ($_POST['op'] == 'mem') {
                $stat = array(
                        Mem::count(),
                        Mem::whereRaw("to_days(from_unixtime(createtime))=to_days(now())")->count(),
                        Mem::whereRaw("date_format(from_unixtime(createtime),'%Y%m')=".$lmon)->count(),
                );
            }elseif ($_POST['op'] == 'shop') {
                $stat = array(
                        Agent::count(),
                        Agent::whereRaw("to_days(from_unixtime(createtime))=to_days(now())")->count(),
                        Agent::whereRaw("date_format(from_unixtime(createtime),'%Y%m')=".$lmon)->count(),
                );
            }
            wxresp(1,array('stat'=>$stat));
        }
        return $this->V("index",['types'=>array(
                ['总交易额','money','currency-cny','cyan',7],
                ['总订单数','order','cart','warning',5],
                ['总代理数','shop','account-check','success',6],
                ['总用户数','mem','account-multiple','primary',6],
        )]);
    }
}